﻿using huqiang.Core.HGUI;
using huqiang.Core.Line;
using huqiang.UIEvent;
using huqiang.UIModel;
using System.Collections.Generic;
using UnityEngine;

public class StartPage : UIPage
{
    StartPageView view;
    class ItemView
    {
        public UserEvent Image;
        public Text Text;
    }
    public override void Initial(UIElement parent, object dat = null)
    {
        base.Initial(parent, dat);
        LoadUI("StartPage");
        view = new StartPageView();
        view.Matching(Main);
        InitialEvent();
        InitialData();
    }
    void InitialEvent()
    {
        var middle = new HotConstructor<ItemView, string>(ItemUpdate).middle;
        view.ScrollX.SetItemUpdate(middle);
        view.ScrollY.SetItemUpdate(middle);
        view.GridScroll.SetItemUpdate(middle);
        view.Last.Click = (o, e) => { LoadPage<DropExPage>(); };
        view.Next.Click = (o, e) => { LoadPage<DrawPage>(); };
    }
    public void InitialData()
    {
        int uid = UIManager.FindUID("Items/ItemTemplate");
        view.ScrollY.ItemTemplateID = uid;
        view.ScrollX.ItemTemplateID = uid;
        view.GridScroll.ItemTemplateID = uid;

        List<string> data = new List<string>();
        for (int i = 1000; i < 1200; i++)
            data.Add(i.ToString() + "😄");
        var scrolly = view.ScrollY;
        scrolly.BindingData = data;
        scrolly.Refresh();

        var scrollx = view.ScrollX;
        scrollx.BindingData = data;
        scrollx.Refresh();

        var grid = view.GridScroll;
        grid.Column = 10;
        grid.BindingData = data;
        grid.Refresh();
        //view.last.userEvent.Click = (o, e) => { LoadPage<TreeViewPage>(); };

        Beeline beeline = new Beeline();
        beeline.lineBase.Color = Color.red;
        beeline.lineBase.Width = 2;
        beeline.Start.x = -100;
        beeline.End.x = 100;
        beeline.End.y = 100;

        ArcLine arc = new ArcLine();
        arc.Closed = true;
        arc.lineBase.Color = Color.green;
        arc.lineBase.Width = 2;
        arc.Pos = new Vector2(0, 220);
        arc.Scale = new Vector2(1, 0.6f);
        arc.Dic = 44;
        arc.Angle = 160;
        arc.Precision = 0.05f;
        arc.Radius = 300;

        BzierLine bzier = new BzierLine();
        bzier.lineBase.Color = Color.blue;
        bzier.lineBase.Width = 2;
        bzier.A = new Vector2(-11, -22);
        bzier.B = new Vector2(0, -220);
        bzier.C = new Vector2(100, 46);
        bzier.Precision = 0.04f;

        view.LineA.AddLine(ref beeline);
        view.LineA.AddLine(ref arc);
        view.LineA.AddLine(ref bzier);
    }
    void ItemUpdate(ItemView item, string dat, int index)
    {
        item.Text.text = dat;
        item.Image.DataContext = index;
        item.Image.Click = ItemClick;
    }
    void ItemClick(UserEvent user, UserAction action)
    {
        Debug.Log("Item: " + (int)user.DataContext + " Click !");
    }
}